package com.yan.service;

import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.yan.comm.JacksonsUtil;
 

 
//@Component
public class RedisCache {

	private Logger log = Logger.getLogger(RedisCache.class);

	@Autowired
	private BaseRedisClient client;

	/**
	 * 将一个对象转换成json字符串放进缓存中
	 * 
	 * @param obj
	 *            缓存对象
	 * @param key
	 *            键
	 * @param expireSecond
	 *            失效秒数
	 */
	public void set(Object obj, String key, int expireSecond) {
		try {
			client.set(key, JacksonsUtil.obj2json(obj));
			client.expire(key, expireSecond);
		} catch (Exception e) {
			log.error("缓存存入失败,异常信息：" + e.getMessage());
		}

	}

	/**
	 * 从缓存中取出字符串
	 * 
	 * @param key
	 * @return
	 */
	public String get(String key) {
		String json = null;
		try {
			json = client.get(key);

		} catch (Exception e) {
			log.error("缓存取出失败,异常信息：" + e.getMessage());

		}
		return json;
	}

	/**
	 * 从缓存中取出对象
	 * 
	 * @param key
	 * @param clazz
	 * @return
	 */
	public <T> T get(String key, Class<T> clazz) {
		T t = null;
		try {
			String json = client.get(key);
			if (json != null) {
				t = JacksonsUtil.json2pojo(json, clazz);
			}
		} catch (Exception e) {
			log.error("缓存取出失败,异常信息：" + e.getMessage());

		}
		return t;
	}

	/**
	 * 从缓存中取出对象流
	 * 
	 * @param key
	 * @param clazz
	 * @return
	 */
	public byte[] getByte(String key) {
		byte[] value = null;
		try {
			value = client.getByte(key);
		} catch (Exception e) {
			log.error("缓存取出失败,异常信息：" + e.getMessage());

		}
		return value;
	}

	/**
	 * 从缓存中取出对象
	 * 
	 * @param key
	 * @param clazz
	 * @return
	 */
	public <T> Map<String, T> getMap(String key, Class<T> clazz) {
		Map<String, T> map = null;
		try {
			String json = client.get(key);
			if (json != null) {
				map = JacksonsUtil.json2map(json, clazz);
			}
		} catch (Exception e) {
			log.error("Map缓存取出失败,异常信息：" + e.getMessage());

		}
		return map;
	}

	/**
	 * 从缓存中取出对象List
	 * 
	 * @param key
	 * @param clazz
	 * @return
	 */
	public <T> List<T> getList(String key, Class<T> clazz) {
		List<T> list = null;
		try {
			String json = client.get(key);
			if (json != null) {
				list = JacksonsUtil.json2list(json, clazz);
			}
		} catch (Exception e) {
			log.error("List缓存取出失败,异常信息：" + e.getMessage());

		}
		return list;
	}

	/**
	 * 往set缓存中存入字符串
	 * 
	 * @param key
	 * @param members
	 * @return
	 */
	public long sadd(String key, int expireSecond, String... members) {
		try {
			long result = client.sadd(key, members);
			client.expire(key, expireSecond);
			return result;
		} catch (Exception e) {
			log.error("set缓存存入失败,异常信息：" + e.getMessage());
		}
		return 0l;
	}

	/**
	 * 判断set缓存中是否存在某字符串
	 * 
	 * @param key
	 * @param member
	 * @return
	 */
	public boolean sismember(String key, String member) {
		try {
			return client.sismember(key, member);
		} catch (Exception e) {
			log.error("判断字符串是否在set缓存中存在 失败,异常信息：" + e.getMessage());
		}
		return false;
	}

	/**
	 * 设置超时
	 * 
	 * @category TODO
	 * @since
	 * @author xuqh
	 * @date 2016-9-22 下午3:29:02
	 * @param key
	 * @param expire
	 */
	public void expire(String key, int expire) {
		try {
			client.expire(key, expire);
		} catch (Exception e) {
			log.error("设置缓存超时失败,异常信息：" + e.getMessage());
		}
	}

	public Long incr(String key, int expire) {
		Long value = 0l;
		try {
			value = client.incr(key);
			client.expire(key, expire);
		} catch (Exception e) {
			log.error("设置缓存数值自增1失败，异常信息：" + e.getMessage());
		}
		return value;
	}

	public Long decr(String key, int expire) {
		Long value = 0l;
		try {
			value = client.decr(key);
			client.expire(key, expire);
		} catch (Exception e) {
			log.error("设置缓存数值减1失败，异常信息：" + e.getMessage());
		}
		return value;
	}

	/**
	 * 从缓存中取出对象
	 * 
	 * @param key
	 * @param clazz
	 * @return
	 */
	public String getIncr(String key) {
		String value = null;
		try {
			value = client.get(key);
		} catch (Exception e) {
			log.error("Map缓存取出失败,异常信息：" + e.getMessage());

		}
		return value;
	}
}
